package Controller;

import Entity.Customer;

import java.util.Date;

import Entity.CasualSubscription;
import Entity.DisabledPlace;
import Entity.FullSubscription;
import Entity.OneTimeCostumerOrder;
import Entity.Order;
import Entity.ParkingLot;
import Entity.ParkingLotManager;
import Entity.ParkingLotWorker;
import Entity.Prices;
import Entity.Subscription;
import Entity.Worker;
import Entity.Worker.Role;
import GUI.FrameIS;
import GUI.FrameSystem;

public class Client {
	public Client(){};
	public static void saveSubscriptionToDB(Object sub)	//save new subscription (should check if its full or not)
	{
		if(sub instanceof FullSubscription)
			DBcontroller.saveFullSub((FullSubscription)sub);
		else
			DBcontroller.saveCasualSub((CasualSubscription)sub);
	}
	public static void UpdateParkingLotToDB(Object pl)	//update change in a parking lot
	{
		DBcontroller.saveParkingLot((ParkingLot)pl);
	}
	public static String getLoggedIn()	//in the system main
	{
		return FrameSystem.getLoggedIn();
	}
	public static int getParkingLot()	//in the information station main
	{
		return FrameIS.getParkingLot();
	}
	
	public static String getParkingLotNameByNumber(int pNum)	//can use "getParkingLots_sorted()"
	{
		ParkingLot[] plots=getParkingLots_sorted();
		for (int i=0; i<plots.length;i++)
			if (plots[i].getParking_lot_number()==pNum)
				return plots[i].getParking_lot_name();
		return null;
	}
	
	public static ParkingLot[] getParkingLots_sorted()	//parking lots are sorted by parking lot number(parking lot i is at [i-1])
	{
		return DBcontroller.getParkingLots_sorted();
	}
	
	public static FullSubscription  getFullSubscriptionByNumber(int subscription_number) //can use "getFullSubscriptions()"
	{
		FullSubscription[] sub=DBcontroller.getFullSubscriptions();
		for(int i=0; i<sub.length; i++)
			if(Integer.valueOf(sub[i].getSubscription_number())==subscription_number)
				return sub[i];
		return null;
	}
	
	
	public static CasualSubscription  getCasualSubscriptionByNumber(int subscription_number) //can use "getCasualSubscriptions()"
	{
		CasualSubscription[] sub=DBcontroller.getCasualSubscriptions();
		for(int i=0; i<sub.length; i++)
			if(Integer.valueOf(sub[i].getSubscription_number())==subscription_number)
				return sub[i];
		return null;
	}
	
	public static Order [] getOrders()
	{
		return DBcontroller.getOrders();
	}
	
	public static FullSubscription [] getFullSubscriptionById(String loggedIn) ////can use "getFullSubscriptions()"
	{
		FullSubscription[] sub=DBcontroller.getFullSubscriptions(); int num=0;
		for(int i=0; i<sub.length; i++)
			if(sub[i].equals(loggedIn))
				num++;
		if(num==0) return null;
		FullSubscription[] arr=new FullSubscription[num];
		for(int i=0, j=0; i<sub.length; i++)
			if(sub[i].equals(loggedIn))
				{arr[j]=sub[i]; j++;}
		return arr;
	}
	public static CasualSubscription [] getCasualSubscriptionById(String loggedIn) //can use "getCasualSubscriptions()"
	{
		CasualSubscription[] sub=DBcontroller.getCasualSubscriptions(); int num=0;
		for(int i=0; i<sub.length; i++)
			if(sub[i].equals(loggedIn))
				num++;
		if(num==0) return null;
		CasualSubscription[] arr=new CasualSubscription[num];
		for(int i=0, j=0; i<sub.length; i++)
			if(sub[i].equals(loggedIn))
				{arr[j]=sub[i]; j++;}
		return arr;
	}
	public static void UpdateOrderInDB(Object order) {	//ca use "getOrders()"
		DBcontroller.deleteOrder((Order) order);
		DBcontroller.saveOrder((Order) order);
		
	}

	public static Prices getCurrentPrices(int parkingLot) {		//get the current used prices record of the parking lot (the prices record with the latest date)
		return DBcontroller.getCurrentPrices(parkingLot);
	}
	public static void saveOneTimeCostumerToDB(OneTimeCostumerOrder order) {
		DBcontroller.saveOneTimeCustomerOrder(order);
		
	}
	public static ParkingLotWorker getPlWorkerByID(String id) {
		Worker[] w=DBcontroller.getWorkers();
		for(int i=0; i<w.length; i++)
		{
			if(id.equals(w[i].getWorkerID()) && w[i].getR()==Role.parkingLotWorker)
			{
				ParkingLotWorker n=new ParkingLotWorker(w[i].getWorkerID(),w[i].getFirstName(),w[i].getLastName(),w[i].getEmail(),w[i].getPassword(),DBcontroller.getParkingLotOfWorker(id));
				return n;
			}
		}
		return null;
		
	}
	public static void saveDisabledPlaceToDB(DisabledPlace dp) 
	{
		DBcontroller.saveDisabledPlace(dp);
	}
	public static DisabledPlace getCurrentDisabledParkingSpot(int[] place,int parkingLot) 
	{	//return the disabledPlace record with the input details THAT DOESNT HAVE END DATE
		return DBcontroller.getCurrectDisabledParkingSpot(place,parkingLot);
	}
	
	public static void updateDisabledSpot(DisabledPlace dp) 
	{
		DBcontroller.deleteDisabledPlace(dp);
		DBcontroller.saveDisabledPlace(dp);
	}
	public static Customer[] getCustomers(){
		return DBcontroller.getCustomers();
		}
	public static void updateCustomer(Customer c) {
		DBcontroller.deleteCustomer(c);
		DBcontroller.saveCustomer(c);
	}

	
	public static void updateFullSubscription(FullSubscription fullSubscription) 
	{
		DBcontroller.deleteFullSub(fullSubscription);
		DBcontroller.saveFullSub(fullSubscription);
	}
	public static void updateCasualSubscription(CasualSubscription casualSubscription) 
	{
		DBcontroller.deleteCasualSub(casualSubscription);
		DBcontroller.saveCasualSub(casualSubscription);	
	}
	public static FullSubscription[] getFullSubscriptions() {
		return DBcontroller.getFullSubscriptions();
	}
	public static CasualSubscription[] getCasualSubscriptions() {
		return DBcontroller.getCasualSubscriptions();
	}
	public static OneTimeCostumerOrder[] getOneTimeCustomerOrders() 
	{
		return DBcontroller.getOneTimeCustomerOrder();
	}
	public static void updateOneTimeCustomerOrder(OneTimeCostumerOrder oneTimeCostumerOrder) 
	{
		DBcontroller.deleteOneTimeCustomerOrder(oneTimeCostumerOrder);
		saveOneTimeCostumerToDB(oneTimeCostumerOrder);
	}
	public static void saveOrderToDB(Order order) {
		DBcontroller.saveOrder(order);
		
	}
	public static ParkingLotManager getPlManagerByID(String id) {
		Worker[] w=DBcontroller.getWorkers();
		for(int i=0; i<w.length; i++)
		{
			if(id.equals(w[i].getWorkerID()) && w[i].getR()==Role.parkingLotManager)
			{
				ParkingLotManager n=new ParkingLotManager(w[i].getWorkerID(),w[i].getFirstName(),w[i].getLastName(),w[i].getEmail(),w[i].getPassword(),DBcontroller.getParkingLotOfWorker(id));
				return n;
			}
		}
		return null;
		
	}
	
	

}
